package com.example.about_example.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/login")
    public boolean login(String username, String password, HttpSession session) {
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) { //判空
            return false;
        }
        if ("admin".equals(username) && "666666".equals(password)) {
            //设置session存储用户名
            session.setAttribute("username", "admin");
            System.out.println("登录成功");
            return true;
        }
        return false;
    }

    @RequestMapping("/getInfo")
    public String userInfo(HttpServletRequest request) {
        //从session获取登录用户
        HttpSession session = request.getSession(false);
        String username = null;
        if (session != null) {
            username = (String) session.getAttribute("username");
        }
        return username;
    }
}
